package dev.kaayf.examples;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;

import dev.kaayf.db.NoteDb;
import dev.kaayf.utils.Note;

public class EditActivity extends AppCompatActivity {
    private NoteDb noteDb;
    private ImageView noteCover, backButton,saveButton;
    private TextView noteTitle, noteOwner, noteDate, wordsLength;
    private EditText noteContent;
    private int wordsCount;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_edit);
        findViews();

        noteDb = new NoteDb(EditActivity.this);

        Note note = (Note) getIntent().getSerializableExtra("note");
        boolean from = getIntent().getBooleanExtra("new", false);

        String context = noteDb.getNoteContextById(note.getNoteId());
        if(context == null){
            context = "";
        }

        // set image first
        if (note.getNoteCover() == null) {
            noteCover.setImageResource(R.drawable.book);
        } else {
            Glide.with(this).load(note.getNoteCover()).into(noteCover);
        }

        noteOwner.setText("Kaayf");
        noteTitle.setText(note.getNoteName());
        noteDate.setText(note.getNoteDate());
        noteContent.setText(context);
        wordsLength.setText(context.length() + " words");

        // for save button
        saveButton.setOnTouchListener((view, motionEvent) -> {
            String saveContent = noteContent.getText().toString();
            Log.i("EditActivity",saveContent);
            noteDb.updateNoteContext(note.getNoteId(), saveContent);
            Toast.makeText(EditActivity.this, "Content saved", Toast.LENGTH_SHORT).show();
            return false;
        });

        // count words when every button pressed
        noteContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                wordsCount = editable.length();
                wordsLength.setText(wordsCount + " words");
            }

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                wordsCount = charSequence.length();
            }
        });

        // bind Touch Listener for button back to last page
        backButton.setOnTouchListener((view, motionEvent) -> {

            // if true -> come from create activity else main
            if(from){
                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }else{
                finish();
            }
            return true;
        });

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    private void findViews() {
        noteCover = findViewById(R.id.edit_layout_note_cover);
        noteTitle = findViewById(R.id.edit_layout_note_title);
        noteOwner = findViewById(R.id.edit_layout_note_owner);
        noteDate = findViewById(R.id.edit_layout_note_date);
        wordsLength = findViewById(R.id.edit_layout_note_words);
        noteContent = findViewById(R.id.edit_layout_note_content);
        backButton = findViewById(R.id.edit_layout_back);
        saveButton = findViewById(R.id.edit_layout_save);
    }
}